<template>
  <view class="page">
    <view class="head">
      <view style="display: flex;">
        <view class="name" style="color: #333333;">{{ couponInfo.couponName !== undefined ? '领取满减券' : '暂无可以领取的优惠券' }}</view>
        <view class="name" style="color: #EE751E;"  v-if="couponInfo.couponName !== undefined">（{{ couponInfo.couponName }}）</view>
      </view>
      <view class="tips">让您的爱宠享受更多优惠服务～～</view>
    </view>
    <view class="button" @click="collectCoupon(couponInfo)" v-if="couponInfo.couponName !== undefined">立即领取</view>
  </view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app'
import { ref, getCurrentInstance } from 'vue'
import { storage, urlGet } from '@/utils/utils'
import { TOKEN } from '@/assets/js/config'
const { $https } = getCurrentInstance().appContext.config.globalProperties
const pagination = ref({
  page: 0,
  more: true,
  total: 0
})
const couponInfo = ref({})
const pageOption = ref({})
onLoad((option) => {
  pageOption.value = option
  initData()
})
const initData = () => {
// 通过扫码进入
  $https({
    url: `/common/param/${pageOption.value.scene}`,
    type: ''
  }).then(res => {
    if (res.code === 0) {
      const couponId = urlGet('couponId', '?' + res.data)
      $https({
        url: '/app/couponInfo/getSingleCoupon',
        type: '',
        data: {
          page: pagination.value.page + 1,
          pageSize: 10,
          couponId
        }
      }).then(res2 => {
        if (res2.code === 0) {
          if (res2.data.items.length > 0) {
            couponInfo.value = res2.data.items[0]
          }
        }
      })
    }
  })
}
const collectCoupon = (item) => {
  if (storage.get(TOKEN)) {
    $https({
      url: '/app/couponInfo/collectSingleCoupon',
      type: '',
      data: item,
      method: 'post'
    }).then(res => {
      if (res.code === 0) {
        uni.showToast({
          title: '操作成功',
          icon: 'none'
        })
        setTimeout(() => {
          uni.redirectTo({
            url: '/user/coupon?type=1'
          })
        }, 1000)
      }
    })
  } else {
    uni.navigateTo({
      url: `/pages/login?redirect=${encodeURIComponent('/pages/getSingleCoupon?scene=' + pageOption.value.scene)}`
    })
  }
}
</script>

<style scoped lang="scss">
.page{
  background-image: url('https://img.qumoyugo.com/img/UY1984XNROWL5B98DKGI1689228239517.png');
  background-size: 100% 100%;
  height: 100vh;

  .head {
    display: inline-block;
    margin-top: 55.56rpx;
    margin-left: 66.67rpx;
  }

  .name {
    font-size: 42rpx;
    font-weight: bold;
  }

  .tips {
    font-size: 32rpx;
    font-weight: 300;
    color: #333333;
    margin-top: 31.25rpx
  }
}
.button {
  width: 389rpx;
  height: 111rpx;
  background: #E9EDFF;
  border: 1px solid #2F2E41;
  border-radius: 56rpx;
  font-size: 35rpx;
  font-weight: bold;
  color: #333333;
  line-height: 100rpx;
  text-align: center;
  bottom: 0;
  position: fixed;
  margin: 0 180.56rpx 197.92rpx 180.56rpx;
}
.no-data{
  margin-top: 30%;
  text-align: center;
}
</style>
